<template>
  <div id="student">
    <el-row class="padding-50">
      <el-col :span="24">
        <ul class="list">
          <li class="logo">
            <i class="iconfont icon-kaoshi"></i>
            <span>DORO-Learning-Online</span>
          </li>
          <li><router-link :to="`/student/learn/${courseId}`">章节学习</router-link></li>
          <li class="logo">课程资料</li>
          <li><router-link :to="`/student/learn_info/${courseId}`">课程信息</router-link></li>
          <li><router-link :to="`/student/AIhelper/${courseId}`">智能学习助手</router-link></li>
          <li><router-link to="/student/message">我的消息</router-link></li>
          <li><router-link to="/student">返回首页</router-link></li>
          <li class="right" @mouseenter="flag = true" @mouseleave="flag = false">
            <a href="javascript:;">
              <i class="iconfont icon-Userselect icon"></i>{{ user.userName }}
            </a>
            <div class="msg" v-if="flag">
              <p @click="manage">管理中心</p>
              <p class="exit" @click="exit">退出</p>
            </div>
          </li>
        </ul>
      </el-col>
    </el-row>

    <div class="main">
      <h2>课程资料</h2>
      <el-table v-if="fileList.length > 0" :data="fileList" style="width: 100%" v-loading="loading">
        <el-table-column prop="path" label="文件名">
          <template #default="{ row }">
            <span class="file-link" @click="confirmDownload(row.path)">
              {{ extractFileName(row.path) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" width="600" />
        <el-table-column prop="createTime" label="上传时间" width="180" />
        <el-table-column prop="type" label="类型" width="120" />
      </el-table>
      <div v-else class="empty-hint">暂无课程资料</div>

      <el-dialog
        v-model="downloadDialogVisible"
        title="确认下载"
        width="30%"
      >
        <span>确定要下载该文件吗？</span>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="downloadDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="downloadConfirmed">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>

    <router-view></router-view>
    <v-footer></v-footer>
  </div>
</template>

<script>
import myFooter from '@/components/student/myFooter'
import { mapState } from 'vuex'

export default {
  components: {
    'v-footer': myFooter
  },
  data() {
    return {
      flag: false,
      user: {},
      courseId: null,
      fileList: [],
      loading: false,
      downloadDialogVisible: false,
      selectedFilePath: ''
    }
  },
  created() {
    this.userInfo()
    this.courseId = this.$route.params.courseId
    this.loadAllCourseFiles()
  },
  methods: {
    userInfo() {
      const name = this.$cookies.get('cname')
      const id = this.$cookies.get('cid')
      this.user = { userName: name, studentId: id }
    },
    exit() {
      this.$cookies.remove('cname')
      this.$cookies.remove('cid')
      this.$router.push('/')
    },
    manage() {
      this.$router.push('/student/manager')
    },
    loadAllCourseFiles() {
      this.loading = true
      this.$axios.get('/api/course/file/page', {
        params: {
          course: this.courseId,
          pageNum: 1,
          pageSize: 1000
        }
      }).then(res => {
        console.log('文件接口响应：', res.data)
        if ((res.data.code === 0 || res.data.code === 200) && res.data.data && Array.isArray(res.data.data.records)) {
          this.fileList = res.data.data.records
        } else {
          this.$message.error(res.data.msg || '加载资料失败')
        }
      }).catch(() => {
        this.$message.error('请求失败')
      }).finally(() => {
        this.loading = false
      })
    },
    extractFileName(path) {
      // Handles both URLs and simple paths
      return path.substring(path.lastIndexOf('/') + 1)
    },
    confirmDownload(path) {
      console.log('点击下载：', path)
      this.selectedFilePath = path
      this.downloadDialogVisible = true
    },
    downloadConfirmed() {
      this.downloadDialogVisible = false
      // The path from the API is a full URL, so it can be opened directly.
      window.open(this.selectedFilePath, '_blank')
    }
  },
  computed: mapState(['isPractice'])
}
</script>

<style scoped>
.right .icon {
  margin-right: 6px;
}
#student .padding-50 {
  margin: 0 auto;
  padding: 0 50px;
  box-shadow: 0 0 10px 4px rgba(1, 149, 255, 0.1);
  background-color: #fff;
}
.list a {
  text-decoration: none;
  color: #334046;
}
li {
  list-style: none;
  height: 60px;
  line-height: 60px;
}
#student .list {
  display: flex;
}
#student .list li {
  padding: 0 20px;
  cursor: pointer;
}
#student .list li:hover {
  background-color: #0195ff;
  transition: all 0.3s ease;
}
#student .list li:hover a {
  color: #fff;
}
#student .list .right {
  margin-left: auto;
  position: relative;
}
#student .list li.right:hover a {
  color: #000;
}
#student .list .logo {
  display: flex;
  font-weight: bold;
  color: #2f6c9f;
}
#student .list .logo i {
  font-size: 50px;
}
.right .msg {
  text-align: center;
  position: absolute;
  top: 60px;
  right: 0;
  display: flex;
  flex-direction: column;
  border-radius: 2px;
  border-bottom: 3px solid #0195ff;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
}
.right .msg p {
  height: 40px;
  line-height: 40px;
  width: 105px;
}
.right .msg p:hover {
  background-color: #0195ff;
  color: #fff;
}
.main {
  padding: 20px 50px;
  background-color: #f9f9f9;
  min-height: 500px; /* Added for better layout */
}

.file-link {
  color: #409EFF;
  cursor: pointer;
}

.file-link:hover {
  text-decoration: underline;
}

.dialog-footer {
  text-align: right;
}

.empty-hint {
  padding: 30px;
  text-align: center;
  color: #999;
  font-size: 16px;
}
</style>